package com.glamour.hlcity.module.spider.util;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Tag;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;

import com.glamour.hlcity.module.node.filter.TagFirstTextFilter;

/**
 * 节点工具类
 * @author zhaoxing.liuzx
 *
 */
public class NodeUtil {
	
	public static Tag getChildTag(Tag tag,Class<? extends TagNode> clazz){
		return null;
	}
	
	/**
	 * 获取第一个满足条件的节点
	 * @param tag
	 * @param tagName 小写的tag名称
	 * @return
	 */
	public static Node getNode(Tag tag,String tagName){
		NodeList nodeList=tag.getChildren();
		TagFirstTextFilter filter=new TagFirstTextFilter(tagName);
		if(nodeList!=null){
			NodeList findList=childNodeList(nodeList, filter);
			if(findList!=null){
				return findList.elementAt(0);
			}
		}
		return null;
	}
	
	public static Node getNode(Tag tag,Class<?> clazz){
		NodeList nodeList=tag.getChildren();
		NodeClassFilter filter=new NodeClassFilter(clazz);
		if(nodeList!=null){
			NodeList findList=childNodeList(nodeList, filter);
			if(findList!=null){
				return findList.elementAt(0);
			}
		}
		return null;
	}
	
    private static NodeList childNodeList(NodeList nodeList,NodeFilter filter){
		return nodeList.extractAllNodesThatMatch(filter, true);
	}
    
    
}
